<?php
//include_once("includes/global.php");
//include_once("includes/smarty_config.php");
$typeid = !empty( intval($_GET['typeid']) ) ? intval($_GET['typeid'])  : 3 ;
$keyword = stripslashes(htmlspecialchars($_GET['keyword']));
switch($typeid){
    case 1 :
        $table = NEWSD;
        $where = " title like '%$keyword%'";
        $order = " order by nid desc";
        break;
    case 2 :
        $table = SPOT;
        $where = " spot_name like '%$keyword%'";
        $order = " order by id desc";
        break;
    case 3 :
        $table = SETMEAL;
        $where = " pname like '%$keyword%'";
        $order = " group by pid order by id desc";
        break;
    default :
        $table = NEWSD;
        $where = " title like '%$keyword%'";
        $order = " order by nid desc";
        break;
}

include_once("includes/page_utf_class.php");
$page = new Page;
$page->url=$config['weburl'].'/';
$page->listRows= 30;
$sql = "select * from ".$table. " where $where $order";

if(!$page->__get('totalRows'))
{
    $db->query($sql);
    $page->totalRows =$db->num_rows();
}
$sql.=" limit ".$page->firstRow.", ".$page->listRows;
$db->query($sql);
$prol=$db->getRows();
if($typeid == 3){
    foreach($prol as $key => $val){
        $sql = "select * from ".PRO." where id = '{$val['pid']}' ";
        $db->query($sql);
        $pro = $db->fetchRow();
        $prol[$key]['pname'] = $pro['pname'];
        $prol[$key]['ftitle'] = $pro['ftitle'];
        $prol[$key]['reserve_time'] = $pro['reserve_time'];
        $prol[$key]['sell_time'] = $pro['sell_time'];
    }
}
$prolist['list']=$prol;
$prolist['page']=$page->prompt();
$prolist['count']=$page->totalRows;
foreach($prolist['list'] as $key => $val){
    $osql = "select price from ".SETMEAL." where pid = '{$val['pid']}' ";
    $db->query($osql);
    $o_list = $db->getRows();
    $o_list = array_unique(array_column($o_list,'price'));

    $max =array_search(max($o_list),$o_list);

    $min =array_search(min($o_list),$o_list);
    $prolist['list'][$key]['price_list'] = $o_list[$min].'-'.$o_list[$max];

}

$tpl->assign("info",$prolist);

$config['title']=str_replace('[catname]',$cat['cat'],$config['title2']);
$config['keyword']=str_replace('[catname]',$cat['cat'],$config['keyword2']);
$config['description']=str_replace('[catname]',$cat['cat'],$config['description2']);
//======================================================================
$tpl->assign("typeid",$typeid);
$tpl->assign("keyword",$keyword);
//include_once("footer.php");
$tpl->display("search.htm");
?>